#pragma comment (linker, "/STACK:100000000")
#include "testlib.h"
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
using namespace std;
#define N 1000100
#define R 1500
int x[N], y[N], r[N], w[N], ri, rj, h[R][R];
vector <int> m[N], v[2*R];
set <int> s;
set <int>::iterator it;
struct T
{
	int y, i;
	T(int y=0, int i=0): y(y), i(i) {}
};
bool operator <(T a, T b) { return a.y<b.y || (a.y==b.y && a.i<b.i); }
T t[4*R];
bool in(int i, int j)
{
	return (x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])<=r[j]*r[j];
}
bool dfs(int i)
{
	int j, k, l, p, y1, y2;
	for(j=0; j<=2*r[i]; v[j].clear(), j++);
	for(j=0; j<m[i].size(); j++)
	{
		k=m[i][j];
		if(x[k]-r[k]<x[i]-r[i] || x[k]+r[k]>x[i]+r[i]) { ri=i; rj=k; return 1; }
		v[x[k]-r[k]-x[i]+r[i]].push_back(k);
		v[x[k]+r[k]-x[i]+r[i]+1].push_back(-k);
	}
	s.clear();
	for(j=0; j<=2*r[i]; j++)
	{
		for(k=0; k<v[j].size() && s.size()<=2*R; k++)
			if(v[j][k]>0) s.insert(v[j][k]);
			else s.erase(-v[j][k]);
		if(!s.size()) continue;
		for(k=0, it=s.begin(); it!=s.end(); it++)
		{
			l=abs(x[i]-r[i]+j-x[*it]);
			t[k++]=T(y[*it]-h[r[*it]][l], *it);
			t[k++]=T(y[*it]+h[r[*it]][l], N);
		}
		sort(t, t+k);
		y1=y[i]-h[r[i]][abs(j-r[i])];
		y2=y[i]+h[r[i]][abs(j-r[i])];
		for(p=-1, l=0; l<k; l++)
			if(t[l].i==N)
			{
				if(t[l].y>y2) { ri=i; rj=p; return 1; }
				p=-1;
			}
			else
			{
				if(p!=-1) { ri=t[l].i; rj=p; return 1; }
				p=t[l].i;
				if(t[l].y<y1) { ri=i; rj=p; return 1; }
			}
	}
	for(j=0; j<m[i].size(); j++)
		if(dfs(m[i][j])) return 1;
	return 0;
}
int main()
{
	registerValidation();
	int i, j, k, e, c, n;
	string s;
	e=inf.readInt(1, 2000, "R"); inf.readSpace();
	c=inf.readInt(1, 2000, "C"); inf.readEoln();
	for(i=0; i<e; i++)
	{
		s=inf.readWord(); inf.readEoln();
		ensuref(s.size()==c, "all rows must have the length %d", c);
		for(j=0; j<c; j++)
			ensuref(s[j]>='0' && s[j]<='9', "only digits 0-9 are allowed");
	}
	n=inf.readInt(1, 1000000, "N"); inf.readEoln();
	for(i=0; i<R; i++)
		for(k=i, j=0; j<=i; h[i][j]=k, j++)
			for(; k*k+j*j>i*i; k--);
	x[0]=R/1.5;
	y[0]=R/1.5;
	r[0]=R-1;
	j=0;
	w[j++]=0;
	for(i=1; i<=n; i++)
	{
		x[i]=inf.readInt(); inf.readSpace();
		y[i]=inf.readInt(); inf.readSpace();
		r[i]=inf.readInt(); inf.readEoln();
		ensuref(x[i]-r[i]>=1 && x[i]+r[i]<=e && y[i]-r[i]>=1 && y[i]+r[i]<=c, "%d-th circle lies outside the bounds", i);
		if(i>1 && x[i]==x[i-1] && y[i]==y[i-1] && r[i]==r[i-1]) continue;
		for(; !in(i, w[j-1]); j--);
		m[w[j-1]].push_back(i);
		w[j++]=i;
	}
	inf.readEof();
	ensuref(!dfs(0), "%d-th and %d-th are invalid", ri, rj);
	return 0;
}